package com.strawberry.chat2.library.netease.chatRoom;

import android.text.TextUtils;
import android.util.Log;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.chatroom.ChatRoomServiceObserver;
import com.netease.nimlib.sdk.chatroom.model.ChatRoomMessage;
import com.netease.nimlib.sdk.msg.constant.MsgTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class Util {
    public static ReactApplicationContext context = null;
    private final static String TAG = "ChatRoomModule222";

    public static void RegisterMsgListener(){
        NIMClient.getService(ChatRoomServiceObserver.class)
                .observeReceiveMessage(incomingChatRoomMsg, true);
    }

    public static void RemoveMsgListener(){
        NIMClient.getService(ChatRoomServiceObserver.class)
                .observeReceiveMessage(incomingChatRoomMsg, false);
    }

    private static Observer<List<ChatRoomMessage>> incomingChatRoomMsg = new Observer<List<ChatRoomMessage>>() {
        @Override
        public void onEvent(List<ChatRoomMessage> messages) {
            // 处理新收到的消息
            if(messages.get(0).getMsgType() == MsgTypeEnum.text){
                extratMessage(messages);
            }
        }
    };

    //解析消息
    public static void extratMessage(List<ChatRoomMessage> messageList){
        for( int i = 0 ; i < messageList.size() ; i++) {//内部不锁定，效率最高，但在多线程要考虑并发操作的问题。
            if(messageList.get(i).getMsgType() == MsgTypeEnum.text) {
                WritableMap chatLog = new WritableNativeMap();
                //好友用户ID
                String uid = messageList.get(i).getFromAccount();
                chatLog.putString("uid", uid);
                chatLog.putString("nickname", messageList.get(i).getChatRoomMessageExtension().getSenderNick());
                Long time = messageList.get(i).getTime();
                chatLog.putString("time", String.valueOf(time));
                if(messageList.get(i).getRemoteExtension() != null) {
                    if((Integer)messageList.get(i).getRemoteExtension().get("type") == 25){///进场消息
                        try {
                            JSONObject ob = new JSONObject(messageList.get(i).getRemoteExtension().get("data").toString());
                            String content = "欢迎 "+ob.getString("nickname")+" 进入房间";
                            chatLog.putString("content", content);
                            context
                                    .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                                    .emit("onChatroomMessage", chatLog);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }else if((Integer)messageList.get(i).getRemoteExtension().get("type") == 6) {///聊天消息
                        String content = messageList.get(i).getContent();
                        chatLog.putString("content", content);
                        context
                                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                                .emit("onChatroomMessage", chatLog);
                    }else if((Integer)messageList.get(i).getRemoteExtension().get("type") == 8) {//关注消息

                    }else if((Integer)messageList.get(i).getRemoteExtension().get("type") == 3) {//点赞直播

                    }
                }
            }
        }
    }
}
